//
//@File    :	15.4_tvfm.h
//@Time    :	2022/03/08 17:25:42
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	Tv and Remote classes using a friend member
//
//< 共同的友元 >
//函数需要访问两个类的私有函数,应当将函数作为两个类的友元



//Remote类中的一个方法set_chan作为Tv类的友元
#ifndef TVFM_H_
#define TVFM_H_
//为了避免循环依赖
class Tv;//前向声明(forward declaration)

class Remote
{
public:
    enum{Off,On};//开关
    enum{MinVal,MaxVal = 20};//音量
    enum{Antenna,Cable};//有线.天线
    enum{TV,DVD};//TV或DVD
private:
    int mode;
public:
    Remote(int m=TV):mode(m){}
    bool volup(Tv &t);       //prototype only
    bool voldown(Tv &t);
    bool onoff(Tv &t);
    bool chanup(Tv &t);
    bool chandown(Tv &t);
    void set_chan(Tv &t,int c);
    void set_mode(Tv &t);
    void set_input(Tv &t);
};

class Tv
{
private:
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;
    int input;

public:
    friend void Remote::set_chan(Tv &t,int c);
    enum{Off,On};//开关
    enum{MinVal,MaxVal = 20};//音量
    enum{Antenna,Cable};//有线.天线
    enum{TV,DVD};//TV或DVD
//初始化
    Tv(int s = Off, int mc = 125) : state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {}
    void onoff(){state = (state==On)?Off:On;}
    // void onoff(){state^=1;}//简化代码
    bool ison() const{return state==On;}
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode(){mode = (mode == Antenna)?Cable:Antenna;}
    void set_input(){input = (input==TV)?DVD:TV;}
    void settings() const;
};

//remote methods as inline functions
    inline bool Remote::volup(Tv &t){return t.volup();}      
    inline bool Remote::voldown(Tv &t){return t.voldown();}
    inline bool Remote::onoff(Tv &t){t.onoff();}
    inline bool Remote::chanup(Tv &t){t.chanup();}
    inline bool Remote::chandown(Tv &t){t.chandown();}
    inline void Remote::set_chan(Tv &t,int c){t.channel=c;}
    inline void Remote::set_mode(Tv &t){t.set_mode();}
    inline void Remote::set_input(Tv &t){t.set_input();}

#endif	// TVFM_H_